import dayjs from "dayjs";

// 将时间初始化为 2022-09-20 格式
export const format = (date) => {
  return dayjs(+date).format("YYYY-MM-DD");
};
// 将时间转换为几天前，几周前
export const transformTime = (dateTimeStamp) => {
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;
  let year = month * 12;

  if (dateTimeStamp == undefined) {
    return false;
  } else {
    if (typeof dateTimeStamp !== "string") return;
    dateTimeStamp = dateTimeStamp.replace(/\-/g, "/");

    let sTime = new Date(dateTimeStamp).getTime(); //把所输入的时间转化为时间戳

    let now = new Date().getTime(); //获取当前时间的时间戳

    let diffValue = now - sTime;

    if (diffValue < 0) {
      return "刚刚";
      // return "结束日期不能小于开始日期！";
    }

    let yearC = diffValue / year;
    let monthC = diffValue / month;
    let weekC = diffValue / (7 * day);
    let dayC = diffValue / day;
    let hourC = diffValue / hour;
    let minC = diffValue / minute;

    if (yearC >= 1) {
      return parseInt(yearC) + "年前";
    } else if (monthC >= 1) {
      return parseInt(monthC) + "个月前";
    } else if (weekC >= 1) {
      return parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
      return parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
      return parseInt(hourC) + "个小时前";
    } else if (minC >= 1) {
      return parseInt(minC) + "分钟前";
    } else {
      return "刚刚";
    }
  }
};
// 根据时间戳转换为多少天
export const transformUserInfoTime = (dateTimeStamp) => {
  if (!/\D/.test(dateTimeStamp)) {
    dateTimeStamp = dayjs(+dateTimeStamp).format("YYYY-MM-DD  HH:mm:ss");
  }
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;
  let year = month * 12;

  if (dateTimeStamp == undefined) {
    return false;
  } else {
    if (typeof dateTimeStamp !== "string") return;
    dateTimeStamp = dateTimeStamp.replace(/\-/g, "/");

    let sTime = new Date(dateTimeStamp).getTime(); //把所输入的时间转化为时间戳

    let now = new Date().getTime(); //获取当前时间的时间戳

    let diffValue = now - sTime;

    if (diffValue < 0) {
      return "刚刚";
      // return "结束日期不能小于开始日期！";
    }

    let yearC = diffValue / year;
    let monthC = diffValue / month;
    let weekC = diffValue / (7 * day);
    let dayC = diffValue / day;
    let hourC = diffValue / hour;
    let minC = diffValue / minute;

    if (yearC >= 1) {
      return parseInt(yearC) + "年";
    } else if (monthC >= 1) {
      return parseInt(monthC) + "个月";
    } else if (dayC >= 1) {
      return parseInt(dayC) + "天";
    } else if (hourC >= 1) {
      return parseInt(hourC) + "个小时";
    } else if (minC >= 1) {
      return parseInt(minC) + "分钟";
    } else {
      return "刚刚";
    }
  }
};
// 根据时间戳转换为多少天
export const transformHistory = (dateTimeStamp) => {
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;

  if (dateTimeStamp == undefined) {
    return false;
  } else {
    if (typeof dateTimeStamp !== "string") return;
    dateTimeStamp = dateTimeStamp.replace(/\-/g, "/");

    let sTime = new Date(dateTimeStamp).getTime(); //把所输入的时间转化为时间戳

    let now = new Date().getTime(); //获取当前时间的时间戳

    let diffValue = now - sTime;

    if (diffValue < 0) {
      return "今日";
    }

    let monthC = diffValue / month;
    let dayC = diffValue / day;

    if (monthC >= 1) {
      return parseInt(monthC) + "个月前";
    } else if (dayC >= 0 && dayC < 1) {
      return "今日";
    } else if (dayC >= 1 && dayC < 2) {
      return "昨日";
    } else if (dayC >= 2 && dayC < 3) {
      return "前日";
    } else if (dayC >= 3) {
      return parseInt(dayC) + "天前";
    }
  }
};
